---
title: Stream(流)
sidebar_position: 9
---

# Stream

### ToBytes

:::note
将流转为 byte[]
:::

```csharp
var filePath = "./XXXXX";
using var fs = new FileStream(filePath, FileMode.OpenOrCreate);
var bytes = fs.ToBytes();
```

### ToStream

:::note
将byte[] 转为流对象
:::

```csharp
var filePath = "./XXXXX";
using var fs = new FileStream(filePath, FileMode.OpenOrCreate);
var stream = fs.ToBytes().ToStream();
```

### SaveTo

:::note
可以将流对象直接保存为文件
:::

```csharp
var filePath = "./XXXXX";
using var fs = new FileStream(filePath, FileMode.OpenOrCreate);
string toFile = "./PPPPP";
fs.SaveTo(toFile);
fs.ToBytes().SaveTo(toFile);
```

### Seek

:::note
将流指针移动到指定位置
:::

```csharp
var filePath = "./XXXXX";
using var fs = new FileStream(filePath, FileMode.OpenOrCreate);
fs.SeekToEnd();
fs.SeekToOrigin();
fs.SeekTo(0);
```

### Clear

:::note
清空流
:::

```csharp
stream.Clear();
```
